package br.pucrio.inf.lac.konoha.hyuga.core.output;

import java.util.Set;

import br.pucrio.inf.lac.konoha.hyuga.common.Pair;
import br.pucrio.inf.lac.konoha.hyuga.core.Output;
import br.pucrio.inf.lac.konoha.hyuga.events.LocationUpdateEvent;
import br.pucrio.inf.lac.konoha.hyuga.events.cell.CellContentEvent;

/**
 * DG2CEP standard output entry.
 *  
 * This class represent an output entry from the Hyuga EPN, such as cluster formation, merge, dispersion, etc. 
 * The events are stored in the standard {@link Output} queue or pushed to consumer applications for further analysis. 
 * 
 * @author Marcos Roriz <mroriz@inf.puc-rio.br>
 
 */
public class DG2CEPEntry extends OutputEntry {
    private Set<Pair<Integer,Integer>>  clusterCells;
    private CellContentEvent[]          cellContents;
    private String                      strClusterCells;
    private String                      strCentroid;
    
    public DG2CEPEntry() {}
    
    public CellContentEvent[] getCellContents() {
        return cellContents;
    }

    public void setCellContents(CellContentEvent[] cellContents) {
        this.cellContents = cellContents;
    }

    public String getStrClusterCells() {
        return strClusterCells;
    }

    public void setStrClusterCells(String strClusterCells) {
        this.strClusterCells = strClusterCells;
    }
    public Set<Pair<Integer, Integer>> getClusterCells() {
        return clusterCells;
    }
    
    public void setClusterCells(Set<Pair<Integer, Integer>> clusterCells) {
        this.clusterCells = clusterCells;
    }
    
    public String getCentroidSTR() {
        Pair<Double, Double> centroid = clusterCentroid(cellContents);
        return centroid.getFirst() + ", " + centroid.getSecond();
    }

    @Override
    public String toString() {
        return toString(getTimestamp());
    }

    public String toString(long timestamp) {
        Pair<Double, Double> centroid = clusterCentroid(cellContents);
        
        StringBuffer sb = new StringBuffer();
        sb.append(timestamp).append(",");
        sb.append(type).append(",");
        sb.append(strClusterCells).append(",");
        sb.append(size).append(",");
        sb.append("[").append(centroid.getFirst()).append(":").append(centroid.getSecond()).append("],");
        sb.append(strContent);
        return sb.toString();
    }
    
    public Pair<Double,Double> clusterCentroid(CellContentEvent[] cellContents) {
        double lat = 0;
        double lng = 0;
        int size = 0;
        
        if (cellContents != null) {
            for (CellContentEvent cell : cellContents) {
                if (cell != null) {
                    if (cell.getNodes() == null) continue;
                    
                    for (LocationUpdateEvent point : cell.getNodes()) {
                        lat += point.getLatitude();
                        lng += point.getLongitude();
                    }
                    size += cell.size();
                }
            }
        }
    
       if (size == 0) size = 1;
       return new Pair<Double, Double>(lat/size, lng/size);
    }
    
}